#include <cstdio>
#include <cstring>
#include <queue>

const int N = 10005, inf = 0x3fffffff, dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
int n, m, s, t;
int e_cnt = 1, heads[N], h[N], cur[N];

struct Edge {
  int v, nxt, flow;
} e[(N << 1) + (N << 2)];

inline void add(int u, int v, int f) {
  e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt, e[e_cnt].flow = f;
  e[++e_cnt].v = u, e[e_cnt].nxt = heads[v], heads[v] = e_cnt, e[e_cnt].flow = 0;
}

inline bool bfs() {
  std::queue<int> q;
  memset(h, 0, sizeof(h));
  q.push(s);
  h[s] = 1;
  cur[s] = heads[s];
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = heads[u], v; i; i = e[i].nxt) {
      if (!h[v = e[i].v] && e[i].flow) {
        h[v] = h[u] + 1;
        q.push(v);
        cur[v] = heads[v];
        if (v == t) return true;
      }
    }
  }
  return false;
}

int dinic(int u, int flow) {
  if (u == t) return flow;
  int rest = flow, k;
  for (int i = cur[u], v; i && rest; i = e[i].nxt) {
    if (h[v = e[i].v] == h[u] + 1 && e[i].flow) {
      k = dinic(v, std::min(e[i].flow, rest));
      cur[u] = i;
      if (!k) h[v] = 0;
      e[i].flow -= k;
      e[i ^ 1].flow += k;
      rest -= k;
    }
  }
  return flow - rest;
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen("P2774 方格取数问题.in", "r", stdin);
  freopen("P2774 方格取数问题.out", "w", stdout);
#endif
#endif

  scanf("%d%d", &n, &m);
  s = 0, t = n * m + 1;
  int a, nx, ny, ans = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      scanf("%d", &a);
      ans += a;
      if (((i + j) & 1) == 0) {
        add(s, (i - 1) * m + j, a);
        for (int k = 0; k < 4; ++k) {
          nx = i + dx[k], ny = j + dy[k];
          if (nx > 0 && ny > 0 && nx <= n && ny <= m) {
            add((i - 1) * m + j, (nx - 1) * m + ny, inf);
          }
        }
      } else {
        add((i - 1) * m + j, t, a);
      }
    }
  }
  int flow = 0, tmp;
  while (bfs()) {
    while ((tmp = dinic(s, inf))) flow += tmp;
  }
  printf("%d", ans - flow);
  return 0;
}